package cz.muni.fi.pv112.cranesimulator.models.crane;

import com.jogamp.opengl.util.texture.Texture;
import com.jogamp.opengl.util.texture.TextureIO;
import cz.muni.fi.pv112.cranesimulator.models.OBJModel;
import cz.muni.fi.pv112.cranesimulator.models.crane.parts.CarrierPart;
import cz.muni.fi.pv112.cranesimulator.models.crane.parts.LiftingRider;
import cz.muni.fi.pv112.cranesimulator.models.crane.parts.LiftingRod;
import cz.muni.fi.pv112.cranesimulator.models.crane.parts.Magnet;
import cz.muni.fi.pv112.cranesimulator.models.crane.parts.OperatingPart;
import cz.muni.fi.pv112.cranesimulator.utils.loaders.ObjLoader;
import java.io.IOException;
import javax.media.opengl.GL;
import static cz.muni.fi.pv112.cranesimulator.utils.handlers.opengl.TextureHandler.loadTexture;

/**
 *
 * @author Michal Ďuriš
 */
public class Crane {

    private static final int CARRIER_PART = 0;
    private static final int OPERATING_PART = 1;
    private static final int LIFTING_RIDER = 2;
    private static final int LIFTING_ROD = 3;
    private static final int MAGNET = 4;

    private boolean liftingObject = false;
    private Liftable liftedObject = null;

    private Texture texture;

    private final OBJModel[] craneParts = {new CarrierPart(),
                                           new OperatingPart(),
                                           new LiftingRider(),
                                           new LiftingRod(),
                                           new Magnet()};

    public Crane() {
    }

    public void init(GL gl) {
        ObjLoader loader = new ObjLoader();

        for (OBJModel cranePart : craneParts) {
            loader.load(cranePart);
        }

        try {
            texture = loadTexture(gl,
                    "/cz/muni/fi/pv112/cranesimulator/resources/textures/models/crane/Sand.jpg",
                    true, TextureIO.JPG);
        } catch (IOException ex) {
            //@TODO: handle this exception
            System.err.println("Unable to load texture !!! :( ");
        }
    }

    public OBJModel[] getCraneParts() {
        return craneParts;
    }

    public CarrierPart getCarrierPart() {
        return (CarrierPart) craneParts[CARRIER_PART];
    }

    public OperatingPart getOperatingPart() {
        return (OperatingPart) craneParts[OPERATING_PART];
    }

    public LiftingRider getLiftingRider() {
        return (LiftingRider) craneParts[LIFTING_RIDER];
    }

    public LiftingRod getLiftingRod() {
        return (LiftingRod) craneParts[LIFTING_ROD];
    }

    public Magnet getMagnet() {
        return (Magnet) craneParts[MAGNET];
    }

    public Texture getTexture() {
        return texture;
    }

    public void setLiftedObject(Liftable liftedObject) {
        this.liftedObject = liftedObject;
    }

    public Liftable getLiftedObject() {
        return liftedObject;
    }

    public void setLiftingObject(boolean liftingObject) {
        this.liftingObject = liftingObject;
    }

    public boolean isLiftingObject() {
        return liftingObject;
    }

}
